package com.example.xunua_weather;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.xunua_weather.entity.WeatherBean;
import com.google.gson.Gson;


public class SearchCityActivity extends BaseActivity implements View.OnClickListener{
    EditText searchEt;
    ImageView submitIv;
    GridView searchGv;
    String url1 = "http://api.map.baidu.com/telematics/v3/weather?location=";
    String url2 = "&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo";
    String city;
    String[]hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州",
    "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_city);
        searchEt = findViewById(R.id.search_et);
        submitIv = findViewById(R.id.search_submit_iv);
        submitIv.setOnClickListener(this);
        searchGv = findViewById(R.id.search_gv);   // 添加热门城市

        ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.item_hotcity,hotCitys);
        searchGv.setAdapter(adapter);
        //GridView设置监听
        setListener();
    }

    private void setListener() {
        searchGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                city=hotCitys[position];
                String url=url1+city+url2;
                loadData(url);
            }
        });
    }

    /**
     * 点击提交后要做的工作
     * 跳转到首页面，并且多了一个地址
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_submit_iv:
                city=searchEt.getText().toString();
                //查找城市
                String url=url1+city+url2;
                loadData(url);
                break;
        }
    }


    //会话框关闭回调  X-utils里面的东西（俺不会）
    @Override
    public void onSuccess(String s) {
        super.onSuccess(s);
        //解析S
        WeatherBean bean=new Gson().fromJson(s,WeatherBean.class);
        if (bean.getError() == 0) {
            Intent intent=new Intent(this,MainActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            //想要实现通过intent开启一个activity，并将这个activity放至栈底或者清空栈后再把这个activity压进栈去。用
            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            //把搜索功能放置栈底
            intent.putExtra("city",city);
            intent.putExtra("search",true);
            startActivity(intent);
        }else {
            Toast.makeText(this, "暂时未收录此城市天气信息...", Toast.LENGTH_SHORT).show();
        }
    }
}
